
package com.taotao.common.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class HttpClientService {
	
	@Autowired
	private CloseableHttpClient httpclient;
	
	private static final ObjectMapper mapper = new ObjectMapper();
	
	//无参get请求
	public <T> T doGet(String uri, TypeReference<T> type){
			return execute(new HttpGet(uri), type);
	}
	
	//有参get请求
	public <T> T doGet(String uri,Map<String,String> params, TypeReference<T> type){
		try {
			URIBuilder uriBuilder = new URIBuilder(uri);
			if (params != null) {
				for (Map.Entry<String, String> entry : params.entrySet()) {
					uriBuilder.setParameter(entry.getKey(), entry.getValue());
				}
			}
			HttpGet httpGet = new HttpGet(uriBuilder.build());
			return execute(httpGet, type);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//无参post请求
	public <T> T doPost(String uri,TypeReference<T> type){
		HttpPost httpPost = new HttpPost(uri);
			return execute(httpPost,type);
	}
	
	//有参post请求
	public <T> T doPost(String uri,Map<String,String> params,TypeReference<T> type){
		try {
			HttpPost httpPost = new HttpPost(uri);
			ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
			if (params != null) {
				for (Map.Entry<String, String> entry : params.entrySet()) {
					parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
				}
				//构造一个form表单式的实体
				UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
				//将请求实体设置到httpPost对象中
				httpPost.setEntity(formEntity);
				//执行请求
				return execute(httpPost,type);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private <T> T execute(HttpRequestBase request, TypeReference<T> type) {
		try {
			String jsonData = this.httpclient.execute(request, new BasicResponseHandler());
			if (type == null) {
				return (T) jsonData;
			}
			return mapper.readValue(jsonData, type);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	//有参post请求
	public <T> T doJsonPost(String uri,String jsonData,TypeReference<T> type){
		try {
			HttpPost httpPost = new HttpPost(uri);
		
				//将请求实体设置到httpPost对象中
				httpPost.setEntity(new StringEntity(jsonData,ContentType.APPLICATION_JSON));
				//执行请求
				return execute(httpPost,type);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
